package net.wanho.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.wanho.entity.User;
import net.wanho.service.UserService;
import net.wanho.vo.AjaxResult;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Author：汤小洋
 * Date：2023-07-20 9:35
 * Description：<描述>
 */
@Api(tags = "1.用户模块")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @ApiOperation("添加用户")
    @PostMapping
    public AjaxResult add(@ApiParam("用户信息") @RequestBody User user) {
        userService.save(user);
        return AjaxResult.success(user);
    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@ApiParam("用户主键") @PathVariable Long id) {
        userService.removeById(id);
        return AjaxResult.success();
    }

    @ApiOperation("根据id修改用户")
    @PutMapping
    public AjaxResult modifyById(@ApiParam("用户信息") User user) {
        userService.updateById(user);
        return AjaxResult.success();
    }

    @ApiOperation("查询所有用户")
    @GetMapping
    public AjaxResult findAll() {
        return AjaxResult.success(userService.list());
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable Long id) {
        return AjaxResult.success(userService.getById(id));
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public AjaxResult login(@ApiParam("用户名") String username,@ApiParam("密码") String password) {
        return AjaxResult.success(userService.login(username,password));
    }

    @ApiOperation("根据条件查询用户")
    @GetMapping("/query")
    public AjaxResult findByCondition(User user) {
       /*QueryWrapper<User> wrapper = new QueryWrapper<>();
        // wrapper.like("username",user.getUsername()); // 会自动在左右两侧拼接%
        // wrapper.likeRight("username",user.getUsername()); // 在右侧拼接%
        wrapper.likeRight(!StringUtils.isEmpty(user.getUsername()), "username", user.getUsername()) // 动态sql
        // wrapper.ge("id", user.getId());
        // wrapper.between("id", 1, 5);
        .in("id", 1, 5)
        .orderByDesc("id");*/

        // 使用lambda表达式
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(User::getUsername, user.getUsername())
                .or()
                .gt(User::getId, user.getId())
                .eq(User::getPassword, user.getPassword());

        return AjaxResult.success(userService.list(wrapper));
    }

    // @ApiOperation("分页查询用户")
    @ApiIgnore
    @GetMapping("/page")
    public AjaxResult page(User user,@ApiParam("当前页") @RequestParam(defaultValue = "1") Integer pageNum,
                           @ApiParam("每页显示条数") @RequestParam(defaultValue = "5") Integer pageSize) {
        // 分页对象
        Page<User> page = new Page<>(pageNum, pageSize);

        Page<User> p = userService.page(page, new LambdaQueryWrapper<User>()
                .like(!StringUtils.isEmpty(user.getUsername()), User::getUsername, user.getUsername())
                .ge(!ObjectUtils.isEmpty(user.getId()), User::getId, user.getId())
        );

        System.out.println(p == page);

        return AjaxResult.success(p);
    }

}
